package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.bo.PermissionsBo;
import com.cskaoyan.bean.vo.PermissionsVo;
import com.cskaoyan.bean.vo.SystemPermissionsBean;
import com.cskaoyan.mapper.PermissionMapper;
import com.cskaoyan.mapper.RoleMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.System;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @date 2020/12/6 19:41
 */
@Service
public class RoleServiceImpl implements RoleService{
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PermissionMapper permissionMapper;

    @Override
    public ListData<Role> queryRoleList(Integer page, Integer limit, String sort, String order, String name) {
        PageHelper.startPage(page, limit);
        RoleExample roleExample = new RoleExample();
        RoleExample.Criteria criteria = roleExample.createCriteria();
        if (name != null) {
            criteria.andNameLike("%" + name + "%");
        }
        criteria.andDeletedNotEqualTo(true);
        List<Role> roles = roleMapper.selectByExample(roleExample);
        ListData<Role> listData = new ListData<>();
        listData.setItems(roles);

        PageInfo<Role> rolePageInfo = new PageInfo<>(roles);
        long total = rolePageInfo.getTotal();
        listData.setTotal(total);
        return listData;
    }

    @Override
    public Role insertRole(Role role) {
        role.setAddTime(new Date());
        role.setUpdateTime(new Date());
        roleMapper.insertSelective(role);
        return role;
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public void deleteRole(Role role) {
        role.setDeleted(true);
        roleMapper.updateByPrimaryKey(role);
    }

    @Override
    public List<OptionsVo> selectValueAndLable() {
        List<OptionsVo> optionsVoList = roleMapper.selectValueAndLable();
        return optionsVoList;
    }

    @Override
    public int countManager(Integer id) {
        int num = roleMapper.countManager(id);
        return num;
    }

    @Override
    public PermissionsVo selectPermissions(String roleId) {
        PermissionsVo permissionsVo = new PermissionsVo();
        List<String> assignedPermissions= roleMapper.selectAssignedPermissions(roleId);
        for (String assignedPermission : assignedPermissions) {
            if ("*".equals(assignedPermission)) {
                assignedPermissions =roleMapper.selectAssigned();
            }
        }
        List<SystemPermissionsBean> systemPermissionsBeanList = roleMapper.selectSystemPermissions();
        permissionsVo.setSystemPermissions(systemPermissionsBeanList);
        permissionsVo.setAssignedPermissions(assignedPermissions);
        return permissionsVo;
    }

    @Override
    public void deletePermission(PermissionsBo permissionsBo) {
        PermissionExample example = new PermissionExample();
        PermissionExample.Criteria criteria = example.createCriteria();
        criteria.andRoleIdEqualTo(permissionsBo.getRoleId());
        permissionMapper.deleteByExample(example);
    }

    @Override
    public void insertPermission(PermissionsBo permissionsBo) {
        List<String> permissions = permissionsBo.getPermissions();
        List<Permission> permissionList = new ArrayList<>();
        for (String permission : permissions) {
            permissionList.add(new Permission(null,permissionsBo.getRoleId(),permission,new Date(),new Date(),false));

        }
        permissionMapper.batchInsert(permissionList);
    }
}
